package exp.parser;

import grammar.parser.FuncFParams;
import word.parser.WordElement;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class LAndExp extends ExpParser {
    private final ExpParser left;
    private final ExpParser right;

    public LAndExp(ExpParser left, ExpParser right) {
        super(false, left.isConst() && (right == null || right.isConst()));
        this.left = left;
        this.right = right;
    }

    public static LAndExp parseLAndExp() {
        List<ExpParser> expParserList = new ArrayList<>();
        expParserList.add(EqExp.parseEqExp());

        while (getAnalyzingWord().getType() == WordElement.WordType.AND) {
            analyzeNextWord();
            expParserList.add(EqExp.parseEqExp());
        }
        ExpParser left = expParserList.get(0);
        for (int i = 1; i <= expParserList.size(); ++i) {
            ExpParser right = i < expParserList.size() ? expParserList.get(i) : null;
            left = new LAndExp(left, right);
        }
        return (LAndExp) left;
    }

    @Override
    public LVal toLVal() {
        return null;
    }

    @Override
    public Integer compute() {
        int l = left.getValue();
        if (right == null) {
            return l;
        }
        if (l == 0) {
            return 0;
        }

        if (right.getValue() == 0) {
            return 0;
        }
        return 1;
    }

    @Override
    public FuncFParams.FuncFParam checkDimension() {
        return null;
    }
}
